package com.bnuz.vtl.repo;

import com.bnuz.vtl.model.PrivateNode;
import com.bnuz.vtl.model.PublicNode;
import com.bnuz.vtl.model.User;
import org.springframework.data.neo4j.annotation.Query;
import org.springframework.data.neo4j.repository.Neo4jRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

@Repository
public interface UserRepo extends Neo4jRepository<User, String>, BaseRepo<User, String> {
    Optional<User> findByUsername(String username);

    @Query("MATCH (U:User), (S:Subject) " +
            "WHERE U.username = {username} AND S.subjectId = {subjectId} " +
            "CREATE (U)-[r:FOLLOWED]->(b)")
    void followSubject(@Param("username") String username, @Param("subjectId") String subjectId);

    @Query("MATCH (U:User)-[r1:FOLLOWED]->(S:Subject)<-[r:CHILD]-(P:PublicNode) WHERE U.username = {username} return P")
    PublicNode getUserSubjectTree(@Param("username") String username);

    @Query("MATCH (N:PrivateNode)-[r:CREATED]->(U:User) WHERE U.username = {username} RETURN N")
    List<PrivateNode> getUserNodes(@Param("username") String username);
}
